#pragma 
#include<iostream>
#include<queue>
#include<vector>
#include<pthread.h>
#include<unistd.h>
#include"Task.hpp"

struct ThreadInfo
{
    pthread_t tid;
    std::string name;
};
static const int defaultnum=5;
template<class T>
class ThreadPool
{
private:
    void Lock()
    {
        pthread_mutex_lock(&mutex_);
    }
    void UnLock()
    {
        pthread_mutex_unlock(&mutex_);
    }
    void WaitUp()
    {
        //唤醒
        pthread_cond_signal(&cond_);
    }
    //线程休眠
    void ThreadSleep()
    {
        pthread_cond_wait(&cond_, &mutex_);
    }
    bool Isempty()
    {
        return task_.empty();
    }
    std::string GetThreadname(pthread_t tid)
    {
        for(const auto& ti:thread)
        {
            if(ti.tid==tid)
            {
                return ti.name;
            }

        }
        return "node";
    }
public:
    // ThreadPool(int num =defaultnum):thread(num)
    // {
    //     pthread_mutex_init(&mutex_,nullptr);
    //     pthread_cond_init(&cond_,nullptr);
    // }


    //线程该干的 有任务处理  没有就休眠
    //这里可能会报错  方法放在类内 时成员函数 有个隐含默认的this指针 （参数不对）类型配不上
    //一般解决 放在类外 或者static
    static void* HandlerTask(void* args)
    {
        ThreadPool<T>* tp=static_cast<ThreadPool<T>*>(args);
        std::string name=tp->GetThreadname(pthread_self());

        while(true)
        {
            tp->Lock();
            //如果为空就休眠
            while(tp->Isempty())
            {
                tp->ThreadSleep();
            }
            T t=tp->pop();
            tp->UnLock();

            t();
            //std::cout<<"new thread wait task: "<<std::endl;
            std::cout<<name<<"run"<<",result"<<t.GetResult()<<std::endl;
        }
    }

    void Start()
    {
        int num=thread.size();
        for(int i=0;i<num;i++)
        {
            thread[i].name="thread-"+std::to_string(i+1);
            pthread_create(&(thread[i].tid),nullptr,HandlerTask,this);
        }
    }
    T pop()
    {
        T t=task_.front();
        task_.pop();
        return t;
    }
    void Push(const T& t)
    {
        //加锁 释放锁
        Lock();
        task_.push(t);
        WaitUp();
        UnLock();
        //

    }
    static ThreadPool<T>*GetInstance()
    {
        pthread_mutex_lock(&lock);//保证线程安全
        if(nullptr==tp_)//保证单例
        {
            std::cout<<"log:singleton create done first"<<std::endl;
            tp_=new ThreadPool();
        }
        pthread_mutex_unlock(&lock);
        return tp_;
    }
private:
    ThreadPool(int num =defaultnum):thread(num)
    {
        pthread_mutex_init(&mutex_,nullptr);
        pthread_cond_init(&cond_,nullptr);
    }
    ~ThreadPool()
    {
        pthread_mutex_destroy(&mutex_);
        pthread_cond_destroy(&cond_);
    }

    ThreadInfo(const ThreadInfo<T>&)=delete;
    const ThreadPool<T> &operator=(const ThreadPool<T> &)=delete;

    // ThreadPool(const ThreadPool<T> &) = delete;
    // const ThreadPool<T> &operator=(const ThreadPool<T> &) = delete; // a=b=c

private:
    std::vector<ThreadInfo> thread;//将所有线程维护在这里面
    std::queue<T> task_;

    pthread_mutex_t mutex_;
    pthread_cond_t cond_;

    static ThreadPool<T>* tp_;
    static pthread_mutex_t lock;
};

template<class T>
ThreadPool<T>*ThreadPool<T>::tp_=nullptr;

template <class T>
pthread_mutex_t ThreadPool<T>::lock = PTHREAD_MUTEX_INITIALIZER;